#include "swpswimlanegraphicitemcontent.h"

#include "graphicitemcommon.h"
#include "swpswimlanegraphicitem.h"

#include <ieditoritem.h>
#include <palette.h>

#include <QtGui/QPainter>

using namespace core;
using namespace swp;

SwpSwimlaneGraphicItemContent::SwpSwimlaneGraphicItemContent(
    const QString& swimlaneName, QGraphicsItem* parent) :
        QGraphicsRectItem(parent), IGraphicItemContent(),
        m_name(swimlaneName),
        m_nameItem(0),
        m_cropped(false),
        m_border(Qt::black),
        m_background(Qt::white)
{
    m_nameItem = new QGraphicsSimpleTextItem(this);
    m_nameItem->setText(swimlaneName);

    relayout();

    QPen _pen = QPen(QBrush(Qt::transparent), PREFERRED_PEN_SIZE);
    _pen.setCosmetic(true);
    setPen(_pen);

    setFlag(QGraphicsItem::ItemStacksBehindParent, true);

    setZValue(2);
}

SwpSwimlaneGraphicItemContent::~SwpSwimlaneGraphicItemContent()
{

}

void SwpSwimlaneGraphicItemContent::setSwimlaneName(
    const QString& swimlaneName)
{
    m_name = swimlaneName;

    m_nameItem->setText(swimlaneName);

    const int width = boundingRect().width();

    const int pWidth = width -
        (2 * PREFERRED_CONTENT_PADDING);

    if (width > pWidth)
    {
        elide(pWidth);
    }
    else
    {
        m_cropped = false;
    }

    IGraphicEditorItem* parent = dynamic_cast<IGraphicEditorItem*>(
        parentItem());

    Q_ASSERT(parent != 0); // can't be null

    parent->handleContentRefresh();

    relayout();
}

QString SwpSwimlaneGraphicItemContent::swimlaneName() const
{
    return m_name;
}

QGraphicsItem* SwpSwimlaneGraphicItemContent::graphicsItem() const
{
    return const_cast<SwpSwimlaneGraphicItemContent*>(this);
}

void SwpSwimlaneGraphicItemContent::drawThumbnail(
    QPainter& painter, const QRectF& boundingRect)
{
    // do nothing
}

QRectF SwpSwimlaneGraphicItemContent::croppedBoundingRect() const
{
    const QRectF ret;

    return ret;
}

void SwpSwimlaneGraphicItemContent::recheckIfFits()
{
    const int pWidth = boundingRect().width() -
        (2 * PREFERRED_CONTENT_PADDING);

    QFontMetrics metrics(m_nameItem->font());

    const int width = metrics.width(m_name);

    if (width <= pWidth)
    {
        m_nameItem->setText(m_name);

        IGraphicEditorItem* parent = dynamic_cast<IGraphicEditorItem*>(
            parentItem());

        Q_ASSERT(parent != 0); // can't be null

        parent->handleContentRefresh();

        m_cropped = false;
    }
    else
    {
        elide(pWidth);
    }

    relayout();
}

void SwpSwimlaneGraphicItemContent::setPalette(const Palette* const palette)
{
    m_border = palette->borderColor();
    m_background = palette->backgroundColor();
}

bool SwpSwimlaneGraphicItemContent::doActionForProperty(
    const QString& property, const QVariantList& value,
    const core::SerializationEngine& helper)
{
    if (property == "name")
    {
        setSwimlaneName(value.first().toString());
    }

    return true;
}

void SwpSwimlaneGraphicItemContent::deserializationFinished(bool ok)
{
    // do nothing
}

QHash<QString, QVariantList> SwpSwimlaneGraphicItemContent::serializationData(
    const core::SerializationEngine& helper) const
{
    QHash<QString, QVariantList> ret;

    QVariantList list;
    list.append(QVariant(m_name));

    ret["name"] = list;

    return ret;
}

void SwpSwimlaneGraphicItemContent::paint(QPainter* painter,
    const QStyleOptionGraphicsItem* option,
    QWidget* widget)
{
    QRectF rect = boundingRect();

    QGraphicsRectItem::paint(painter, option, widget);

    QPen _pen = pen();
    _pen.setColor(m_border);

    painter->setBrush(QBrush(m_background));
    painter->drawRect(rect);
    painter->setPen(_pen);
    painter->drawLine(rect.bottomLeft(), rect.bottomRight());
}

QRectF SwpSwimlaneGraphicItemContent::boundingRect() const
{
    QRectF ret = m_nameItem->boundingRect();

    SwpSwimlaneGraphicItem* parItem = dynamic_cast<SwpSwimlaneGraphicItem* >(
        parentItem());

    const qreal parWidth = parItem->boundingRectWithoutHandles().width();

    ret.setLeft(PREFERRED_PEN_SIZE);
    ret.setTop(PREFERRED_PEN_SIZE);
    ret.setWidth(parWidth - (2 * PREFERRED_PEN_SIZE));
    ret.setHeight(ret.height() + (2 * PREFERRED_CONTENT_PADDING_SMALL));

    return ret;
}

void SwpSwimlaneGraphicItemContent::relayout()
{
    const QRectF itemRect = m_nameItem->boundingRect();
    const QRectF rect = boundingRect();

    m_nameItem->setPos(rect.width() / 2 - itemRect.width() / 2,
        PREFERRED_CONTENT_PADDING_SMALL);
}

void SwpSwimlaneGraphicItemContent::elide(qreal width)
{
    QFontMetrics metrics(m_nameItem->font());

    const QString elided = metrics.elidedText(
        m_name, Qt::ElideRight, width);

    m_nameItem->setText(elided);

    m_cropped = true;
}
